# 五子棋游戏 HTML 实现

以下是一个完整的五子棋游戏 HTML 实现，所有代码都在一个文件中：

```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>五子棋游戏</title>
    <style>
        body {
            font-family: 'Microsoft YaHei', sans-serif;
            display: flex;
            flex-direction: column;
            align-items: center;
            background-color: #f5f5dc;
            margin: 0;
            padding: 20px;
        }
        
        h1 {
            color: #8B4513;
            margin-bottom: 10px;
        }
        
        .game-container {
            display: flex;
            flex-direction: column;
            align-items: center;
            margin-top: 20px;
        }
        
        .board {
            display: grid;
            grid-template-columns: repeat(15, 40px);
            grid-template-rows: repeat(15, 40px);
            background-color: #DEB887;
            border: 2px solid #8B4513;
            position: relative;
        }
        
        .cell {
            width: 40px;
            height: 40px;
            box-sizing: border-box;
            position: relative;
            cursor: pointer;
        }
        
        .cell::before, .cell::after {
            content: '';
            position: absolute;
            background-color: #000;
        }
        
        .cell::before {
            width: 100%;
            height: 1px;
            top: 50%;
            left: 0;
            transform: translateY(-50%);
        }
        
        .cell::after {
            width: 1px;
            height: 100%;
            left: 50%;
            top: 0;
            transform: translateX(-50%);
        }
        
        .piece {
            position: absolute;
            width: 36px;
            height: 36px;
            border-radius: 50%;
            top: 2px;
            left: 2px;
            z-index: 1;
        }
        
        .black {
            background: radial-gradient(circle at 30% 30%, #666, #000);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.7);
        }
        
        .white {
            background: radial-gradient(circle at 30% 30%, #fff, #ccc);
            box-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
        }
        
        .controls {
            margin-top: 20px;
            display: flex;
            gap: 20px;
        }
        
        button {
            padding: 10px 20px;
            background-color: #8B4513;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        button:hover {
            background-color: #A0522D;
        }
        
        .status {
            margin-top: 15px;
            font-size: 18px;
            font-weight: bold;
            color: #8B4513;
            height: 24px;
        }
        
        .star-point {
            position: absolute;
            width: 8px;
            height: 8px;
            background-color: #000;
            border-radius: 50%;
            transform: translate(-50%, -50%);
            z-index: 1;
        }
    </style>
</head>
<body>
    <h1>五子棋游戏</h1>
    <div class="game-container">
        <div class="status" id="status">黑方回合</div>
        <div class="board" id="board"></div>
        <div class="controls">
            <button id="restart">重新开始</button>
            <button id="undo">悔棋</button>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            const board = document.getElementById('board');
            const statusDisplay = document.getElementById('status');
            const restartButton = document.getElementById('restart');
            const undoButton = document.getElementById('undo');
            
            const BOARD_SIZE = 15;
            let currentPlayer = 'black';
            let gameState = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
            let moveHistory = [];
            let gameOver = false;
            
            // 创建棋盘
            function createBoard() {
                board.innerHTML = '';
                
                // 添加星位点
                const starPoints = [
                    [3, 3], [3, 11], [7, 7], [11, 3], [11, 11]
                ];
                
                starPoints.forEach(([x, y]) => {
                    const starPoint = document.createElement('div');
                    starPoint.className = 'star-point';
                    starPoint.style.left = `${x * 40 + 20}px`;
                    starPoint.style.top = `${y * 40 + 20}px`;
                    board.appendChild(starPoint);
                });
                
                // 创建棋盘格子
                for (let y = 0; y < BOARD_SIZE; y++) {
                    for (let x = 0; x < BOARD_SIZE; x++) {
                        const cell = document.createElement('div');
                        cell.className = 'cell';
                        cell.dataset.x = x;
                        cell.dataset.y = y;
                        cell.addEventListener('click', () => handleCellClick(x, y));
                        board.appendChild(cell);
                    }
                }
            }
            
            // 处理点击事件
            function handleCellClick(x, y) {
                if (gameOver || gameState[y][x] !== null) return;
                
                // 放置棋子
                placePiece(x, y, currentPlayer);
                
                // 检查胜利条件
                if (checkWin(x, y)) {
                    statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}获胜!`;
                    gameOver = true;
                    return;
                }
                
                // 切换玩家
                currentPlayer = currentPlayer === 'black' ? 'white' : 'black';
                statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}回合`;
            }
            
            // 放置棋子
            function placePiece(x, y, player) {
                gameState[y][x] = player;
                moveHistory.push({x, y, player});
                
                const piece = document.createElement('div');
                piece.className = `piece ${player}`;
                
                const cell = document.querySelector(`.cell[data-x="${x}"][data-y="${y}"]`);
                cell.appendChild(piece);
            }
            
            // 检查胜利条件
            function checkWin(x, y) {
                const directions = [
                    [1, 0],   // 水平
                    [0, 1],   // 垂直
                    [1, 1],   // 对角线
                    [1, -1]   // 反对角线
                ];
                
                const player = gameState[y][x];
                
                for (const [dx, dy] of directions) {
                    let count = 1;
                    
                    // 正向检查
                    for (let i = 1; i < 5; i++) {
                        const nx = x + i * dx;
                        const ny = y + i * dy;
                        
                        if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && gameState[ny][nx] === player) {
                            count++;
                        } else {
                            break;
                        }
                    }
                    
                    // 反向检查
                    for (let i = 1; i < 5; i++) {
                        const nx = x - i * dx;
                        const ny = y - i * dy;
                        
                        if (nx >= 0 && nx < BOARD_SIZE && ny >= 0 && ny < BOARD_SIZE && gameState[ny][nx] === player) {
                            count++;
                        } else {
                            break;
                        }
                    }
                    
                    if (count >= 5) return true;
                }
                
                return false;
            }
            
            // 重新开始游戏
            function restartGame() {
                gameState = Array(BOARD_SIZE).fill().map(() => Array(BOARD_SIZE).fill(null));
                moveHistory = [];
                currentPlayer = 'black';
                gameOver = false;
                statusDisplay.textContent = '黑方回合';
                createBoard();
            }
            
            // 悔棋
            function undoMove() {
                if (gameOver || moveHistory.length === 0) return;
                
                const lastMove = moveHistory.pop();
                gameState[lastMove.y][lastMove.x] = null;
                
                const cell = document.querySelector(`.cell[data-x="${lastMove.x}"][data-y="${lastMove.y}"]`);
                cell.innerHTML = '';
                
                currentPlayer = lastMove.player;
                statusDisplay.textContent = `${currentPlayer === 'black' ? '黑方' : '白方'}回合`;
            }
            
            // 事件监听
            restartButton.addEventListener('click', restartGame);
            undoButton.addEventListener('click', undoMove);
            
            // 初始化游戏
            createBoard();
        });
    </script>
</body>
</html>
```

## 游戏功能说明

1. **基本规则**：
   - 15×15 标准五子棋棋盘
   - 黑方先行，轮流落子
   - 先形成五子连线的一方获胜

2. **游戏功能**：
   - 显示当前回合玩家
   - 重新开始游戏按钮
   - 悔棋功能
   - 游戏结束时显示获胜方

3. **界面特点**：
   - 传统木质棋盘风格
   - 棋子有立体效果
   - 棋盘上有标准星位标记

4. **使用方法**：
   - 将代码保存为 HTML 文件
   - 用浏览器打开即可开始游戏
   - 点击棋盘格子放置棋子

您可以根据需要调整样式或添加更多功能，如计时器、AI对手等。